package com.xy.biz.xd.controller;

import com.xy.biz.xd.domain.converter.DocumentFlowConverter;
import com.xy.biz.xd.domain.converter.DocumentReleaseConverter;
import com.xy.biz.xd.domain.query.DocumentFlowQuery;
import com.xy.biz.xd.domain.query.DocumentReleaseQuery;
import com.xy.biz.xd.domain.req.GenerateDocumentRequest;
import com.xy.biz.xd.domain.req.ListLedgerDataRequest;
import com.xy.biz.xd.domain.resp.GetDocumentFlowResponse;
import com.xy.biz.xd.domain.resp.GetDocumentReleaseResponse;
import com.xy.biz.xd.service.DocumentFlowService;
import com.xy.biz.xd.service.DocumentReleaseService;
import com.xy.biz.xd.service.LedgerService;
import com.xy.lang.domain.Page;
import com.xy.lang.domain.Result;
import com.xy.lang.validation.annotation.Validatable;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;

/**
 * @author Greekn Zhang
 * @date 2021/7/1 13:36
 * @description
 */
@Api(tags = "台账服务")
@Slf4j
@RequestMapping("/api/xd/ledger")
@RestController
public class LedgerController {

    @Autowired
    private LedgerService ledgerService;
    @Autowired
    private DocumentFlowService documentFlowService;
    @Autowired
    private DocumentReleaseService documentReleaseService;

    @Validatable
    @ApiOperation("生成表格")
    @PostMapping(value = "/generate")
    public Result<?> getDocumentFlow(@RequestBody GenerateDocumentRequest request) throws IOException {
        return ledgerService.generate(request);
    }

    @ApiOperation("台账变更处理数据")
    @PostMapping(value = "/page_ledger")
    public Result<Page<GetDocumentFlowResponse>> pageLedger(@RequestBody ListLedgerDataRequest request) {
        DocumentFlowQuery query = DocumentFlowConverter.toQuery(request);
        Page<GetDocumentFlowResponse> list = documentFlowService.listLedgerData(query);
        return Result.success(list);
    }

    @ApiOperation("发行台账处理数据")
    @PostMapping(value = "/page_ledger_release")
    public Result<Page<GetDocumentReleaseResponse>> pageLedgerRelease(@RequestBody ListLedgerDataRequest request) {
        //获取所有发放的文件
        DocumentReleaseQuery query = DocumentReleaseConverter.toQuery(request);
        List<GetDocumentReleaseResponse> documentReleaseResponses = DocumentReleaseConverter.toResponse(documentReleaseService.listAllDocumentRelease(query));
        int count = documentReleaseService.countAllDocumentRelease(query);
        return Result.success(new Page<>(documentReleaseResponses, count));
    }

    @ApiOperation("受控台账处理数据")
    @PostMapping(value = "/page_ledger_effect")
    public Result<Page<GetDocumentReleaseResponse>> pageLedgerEffect(@RequestBody ListLedgerDataRequest request) {
        return Result.success(ledgerService.listEffect(request));
    }
}
